package com.example.newsmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.newsmanager.dao.CollectMapper;
import com.example.newsmanager.dao.NewsMapper;
import com.example.newsmanager.entity.Collect;
import com.example.newsmanager.entity.News;
import com.example.newsmanager.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private CollectMapper collectMapper;

//    @Override
//    public List<News> getAllNewsWithAuthor() {
//        return newsMapper.getAllNewsWithAuthor();
//    }
//
//    @Override
//    public List<News> getMyAllNewsService(Integer userId, Integer pages) {
//        Page<News> page = new Page<>(pages, 2);
//        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
//        newsMapper.selectPage(page, queryWrapper.eq("user_id",userId));
//        return page.getRecords();
//    }
//
//    @Override
//    public List<News> getTypeAllNewsService(String newsType, Integer pages) {
//        Page<News> page = new Page<>(pages, 2);
//        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
//        newsMapper.selectPage(page, queryWrapper.eq("news_type",newsType));
//        return page.getRecords();
//    }
//    @Override
//    public String getUserName(String newsId){
//        // 根据新闻ID查询新闻记录，从中获取用户ID
//        QueryWrapper<News> newsQueryWrapper = new QueryWrapper<>();
//        newsQueryWrapper.select("userId").eq("newsId", newsId);
//        News news = newsMapper.selectOne(newsQueryWrapper);
//        if (news != null) {
//            // 根据用户ID查询用户昵称
//            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//            userQueryWrapper.select("nickname").eq("userId", news.getUserId());
//            User user = userMapper.selectOne(userQueryWrapper);
//            if (user != null) {
//                return user.getNickname();
//            }
//        }
//        // 如果未找到用户或新闻记录，返回一个默认值或错误信息
//        return "Unknown User";
//    }
    @Override
    public void addCollect(Collect collect){
        News news = newsMapper.selectById(collect.getNewsId());
        QueryWrapper<Collect> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("news_id",collect.getNewsId());
        Long collectNumber = collectMapper.selectCount(queryWrapper);
        news.setCollectNumber(collectNumber);
        newsMapper.updateById(news);
    }
    @Override
    public Map<String, Object> getCollectList(String userId, Integer pages, Integer size){
        Page<News> page =new Page<>(pages,size);
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("news_id").eq("user_id",userId).orderByDesc("collect_data");

        // 查询符合条件的总记录数
        Long total = collectMapper.selectCount(queryWrapper);
        // 计算总页数
        Long totalPage = total % size == 0 ? total / size : total / size + 1;
        page.setTotal(total);
        page.setPages(totalPage);
        List<Collect> collects = collectMapper.selectList(queryWrapper);
        List<News> newsList = new ArrayList<>();
        for(Collect collect:collects){
            newsList.add(newsMapper.selectById(collect.getNewsId()));
        }
        page.setRecords(newsList);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;

    }
    @Override
    public Map<String, Object> getListById(String userId, Integer pages, Integer size){
        Page<News> page =new Page<>(pages,size);
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).orderByDesc("publish_time");
        newsMapper.selectPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }
    @Override
    public Map<String, Object> searchNews(String keyword, Integer pages, Integer size) {
        Page<News> page = new Page<>(pages,size);
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword).or().like("author", keyword).orderByDesc("publish_time");
        newsMapper.selectPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }
    @Override
    public  Map<String, Object> getNewsListByNumber(Integer pages, Integer size) {
        Page<News> page = new Page<>(pages,size);
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("number")
                    .orderByDesc("publish_time"); // 按浏览量降序排列
        newsMapper.selectPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }
}
